/*
 * Copyright (C) 2009 Show SMS open source project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.bydavy.android.showsms.util;

//~--- non-JDK imports --------------------------------------------------------

import android.content.ContentUris;
import android.content.Context;

import android.database.Cursor;

import android.graphics.Bitmap;

import android.net.Uri;

import android.provider.Contacts.People;
import android.provider.Contacts.Phones;

public class ContactInfos {
    //~--- constructors -------------------------------------------------------

    private ContactInfos() {}

    //~--- methods ------------------------------------------------------------

    public static long findId(Context context, String phoneNumber) {

        // Get the base URI
        Uri myPerson = Uri.withAppendedPath(Phones.CONTENT_FILTER_URL, Uri.encode(phoneNumber));

        // Column to return
        String[] projection = new String[] { Phones.PERSON_ID };
        Cursor cursor       = context.getContentResolver().query(myPerson, projection, null, null, null);

        if (cursor.moveToFirst()) {
            int idColumn = cursor.getColumnIndex(Phones.PERSON_ID);
            Long id      = cursor.getLong(idColumn);

            AppLog.v("Found mId contact: " + id);
            cursor.close();

            return id;
        }

        cursor.close();
        AppLog.v("Not Found mId contact");

        return 0;
    }

    public static String findNameWithAddress(Context c, String address) {
        String name = "";
        long id     = findId(c, address);

        if (id != 0) {
            name = findName(c, id);
        }

        return name;
    }

    public static String findDisplayName(Context context, Long id) {

        // Get the base URI
        Uri myPerson = ContentUris.withAppendedId(People.CONTENT_URI, id);

        // Column to return
        String[] projection = new String[] { People.DISPLAY_NAME };
        Cursor cursor       = context.getContentResolver().query(myPerson, projection, null, null, null);

        if (cursor != null) {
            try {
                if (cursor.getCount() > 0) {
                    cursor.moveToFirst();

                    int displayNameColumn = cursor.getColumnIndex(People.DISPLAY_NAME);
                    String name           = cursor.getString(displayNameColumn);

                    AppLog.v("Found mName contact: " + name);
                    cursor.close();

                    return name;
                }
            } finally {
                cursor.close();
            }
        }

        AppLog.v("Not Found mName contact");

        return "";
    }

    public static String findName(Context context, Long id) {

        // Get the base URI
        Uri myPerson = ContentUris.withAppendedId(People.CONTENT_URI, id);

        // Column to return
        String[] projection = new String[] { People.NAME };
        Cursor cursor       = context.getContentResolver().query(myPerson, projection, null, null, null);

        if (cursor != null) {
            try {
                if (cursor.getCount() > 0) {
                    cursor.moveToFirst();

                    int nameColumn = cursor.getColumnIndex(People.NAME);
                    String name    = cursor.getString(nameColumn);

                    AppLog.v("Found mName contact: " + name);
                    cursor.close();

                    return name;
                }
            } finally {
                cursor.close();
            }
        }

        AppLog.v("Not Found mName contact");

        return "";
    }

    public static Bitmap findPhoto(Context context, Long id) {
        Uri uri = ContentUris.withAppendedId(People.CONTENT_URI, id);

        return People.loadContactPhoto(context, uri, 0, null);
    }
}
